import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'
import user from './modules/user'
import permission from './modules/permission'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    // 储存数据
    name: '大锤',
    student: {
      name: '小红',
      school: {
        name: '黑马程序员',
        addr: {
          city: '广州',
          detail: '珠吉路58号'
        }
      }
    }
  },
  mutations: {
    // 修改 state, 只能做同步修改
    // 所有数据都必须由 mutations 修改
    setName(state, data) {
      state.name = data
    }
  },
  actions: {
    // 如果有异步操作就得放入 actions
    // 异步必须放 actions, 但是actions 其实同步异步动能执行
    asyncSetName(store) {
      setTimeout(() => {
        store.commit('setName', 'actions大锤')
      }, 1000)
    }
  },
  modules: {
    app,
    settings,
    user,
    permission,
    testModule: {
      namespaced: true,
      state: { name: 'aaa' },
      mutations: {
        setNameTest(state) {
          state.name = 'bbbb'
        }
      }
    }
  },
  getters: {
    ...getters,
    studentAddr: state => state.student.school.addr.detail
  }
})

export default store
